package com.ncs.controller.manager_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ncs.entity.PageResult;
import com.ncs.entity.QueryPageBean;
import com.ncs.entity.Result;

import com.ncs.pojo.Contanct;
import com.ncs.pojo.PhoneManager;
import com.ncs.service.ContanctService;
import com.ncs.service.PhoneManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/manager/contanct")
public class ManageContanctController {
    @Autowired
    ContanctService contanctService;
    @PostMapping("/getpage")
    /**分页查询**/
    Result getPage(@RequestBody QueryPageBean queryPageBean){
        Result result = new Result();
        try {
//            当前页
            Integer currentPage = queryPageBean.getCurrentPage();
//            页面大小
            Integer pageSize = queryPageBean.getPageSize();
//            分页查询
            Page<Contanct> adminPage = new Page<Contanct>(currentPage, pageSize);
            QueryWrapper<Contanct> adminQueryWrapper = new QueryWrapper<Contanct>();
            if (queryPageBean.getQueryString() != null) {
                adminQueryWrapper.like("name", queryPageBean.getQueryString()).or().like("email",queryPageBean.getQueryString());
            }
            Page<Contanct> page = contanctService.page(adminPage, adminQueryWrapper);
//            设置分页查询返回值
            PageResult pageResult = new PageResult();
            pageResult.setRows(page.getRecords());
            pageResult.setTotal(page.getTotal());
            result.setData(pageResult);
            result.setFlag(true);
            result.setMessage("查询成功");


        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return  result;

    }
    @PostMapping("/toUpdate")
    /**分页查询**/
    Result toUpdate(@RequestBody Contanct contanct){
        Result result = new Result();
        try {
            UpdateWrapper<Contanct> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",contanct.getId());
            contanctService.update(contanct,updateWrapper);
            result.setFlag(true);
            result.setMessage("更新成功");

        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage("更新失败");
        }
        return  result;

    }
    @PostMapping("/toAdd")
    /**分页查询**/
    Result toAdd(@RequestBody Contanct contanct){
        Result result = new Result();
        try {

            contanctService.save(contanct);
            result.setFlag(true);
            result.setMessage("添加成功");

        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage("添加失败");
        }
        return  result;

    }
    @DeleteMapping("/toDelete/{id}")
    /**分页查询**/
    Result toDelete(@PathVariable int id){
        Result result = new Result();
        try {
            contanctService.removeById(id);
            result.setFlag(true);
            result.setMessage("删除成功");

        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage("删除失败");
        }
        return  result;

    }
}

